/*
 * WeaponpToken.java
 * Copyright 2003 (C) Devon Jones <soulcatcher@evilsoft.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Created on December 15, 2003, 12:21 PM
 *
 * Current Ver: $Revision: 1777 $
 * Last Editor: $Author: jdempsey $
 * Last Edited: $Date: 2006-12-16 22:36:01 -0600 (Sat, 16 Dec 2006) $
 *
 */
package plugin.exporttokens;

import pcgen.cdom.base.Constants;
import pcgen.cdom.enumeration.StringKey;
import pcgen.core.Equipment;
import pcgen.core.Globals;
import pcgen.core.PlayerCharacter;
import pcgen.core.analysis.OutputNameFormatting;
import pcgen.io.ExportHandler;
import pcgen.io.exporttoken.WeaponToken;
import pcgen.util.Delta;

import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

/**
 * <code>ACTypeDroidPCViewerToken</code>.
 * 
 */
public class ACTypeDroidPCViewerToken extends ACToken
{
	/** ACTypeDroidPCViewerToken Token. */
	public static final String TOKEN_NAME = "ACD";

	/**
	 * Gets the token name
	 * 
	 * @return The token name.
	 * @see pcgen.io.exporttoken.Token#getTokenName()
	 */
	public String getTokenName()
	{
		return TOKEN_NAME;
	}

	/**
	 * TODO: get the parsing out of PlayerCharacter - we should know the AC Types here. 
	 * @see pcgen.io.exporttoken.Token#getToken(java.lang.String, pcgen.core.PlayerCharacter, pcgen.io.ExportHandler)
	 */
	@Override
	public String getToken(String tokenSource, PlayerCharacter pc,
		ExportHandler eh)
	{
		return Integer.toString(getACTokenEx(tokenSource, pc));
	}

	/**
	 * Get the AC Token
	 * @param tokenSource
	 * @param pc
	 * @return AC Token
	 */
	public int getACTokenEx(String tokenSource, PlayerCharacter pc)
	{
		String token = tokenSource.substring(4);
		
		int ability = pc.calcACOfType("Ability");
		
		int output = 0;
		if (token.equals("MaxDex"))
		{
			// Remove the Ability, which is capped by armor, from both the total and flatfooted
			// ac, if they are the same, then return the ability.  If the ability is zero,
			// it doesn't matter.
//			if (pc.calcACOfType("Ability") != 0)
//			{
//				int temp1 = pc.calcACOfType("Total") - pc.calcACOfType("Ability");
//				int temp2 = pc.calcACOfType("Flatfooted") - pc.calcACOfType("Ability");
//				if (temp1 == temp2)
//					output = pc.calcACOfType("Ability");
//			}
			output = (pc.calcACOfType("FlatfootedDex") != 0 ? pc.calcACOfType("Ability") : 0);
		}
		else if (token.equals("FlatfootedNoAbility"))
			output = pc.calcACOfType("Total") - ability;
		else if (token.equals("IncludeDex"))
		{
			// As the determination of whether flatfooted is in the miscinfo.lst file,
			// we don't actually know whether we should include Dex into flatfooted,
			// so as long as the ability score is not 0, then we 
			//if (pc.calcACOfType("Ability") != 0)
			//output = (pc.calcACOfType("Total") - pc.calcACOfType("Flatfooted") == 0);
			output = (pc.calcACOfType("FlatfootedDex") != 0 ? 1 : 0);
		}
		else if (token.equals("ShieldlessNoAbility"))
			output = pc.calcACOfType("Total") - ability - pc.calcACOfType("Shield") - pc.calcACOfType("ShieldEnhancement");
		else if (token.equals("TotalNoAbility"))
			output = pc.calcACOfType("Total") - ability;
		else if (token.equals("TouchNoAbility"))
			output = pc.calcACOfType("Touch") - ability;
		else 
			output = pc.calcACOfType(token);
		
		return output;
	}
}
